﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace TrendingReader.Classes
{
    public class CommonFunctions
    {
        public static string ConvertToUnSign(string s)
        {
            string stFormD = s.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();
            for (int ich = 0; ich < stFormD.Length; ich++)
            {
                System.Globalization.UnicodeCategory uc = System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]);
                if (uc != System.Globalization.UnicodeCategory.NonSpacingMark)
                {
                    sb.Append(stFormD[ich]);
                }
            }
            sb = sb.Replace('Đ', 'D');
            sb = sb.Replace('đ', 'd');
            sb = sb.Replace(' ', '-');
            sb = sb.Replace("-----", "-");
            sb = sb.Replace("----", "-");
            sb = sb.Replace("---", "-");
            sb = sb.Replace("--", "-");
            return (sb.ToString().Normalize(NormalizationForm.FormD).ToLower());
        }

        public static void AddMessageToListBox(ListBox listBox, string message)
        {
            if (listBox.InvokeRequired)
            {
                listBox.Invoke((MethodInvoker)(() => listBox.Items.Add(message)));
                listBox.Invoke((MethodInvoker)(() => listBox.SelectedIndex = listBox.Items.Count - 1));
            }
            else
            {
                listBox.Items.Add(message);
                listBox.SelectedIndex = listBox.Items.Count - 1;
            }
        }

        public static void AddMessageToListBox(ListBox listBox, string message, int rowCountToClearListBox)
        {
            if (listBox.InvokeRequired)
            {
                if (rowCountToClearListBox > 0 && listBox.Items.Count == rowCountToClearListBox)
                    listBox.Invoke((MethodInvoker)(() => listBox.Items.Clear()));                   

                listBox.Invoke((MethodInvoker)(() => listBox.Items.Add(message)));
                listBox.Invoke((MethodInvoker)(() => listBox.SelectedIndex = listBox.Items.Count - 1));
            }
            else
            {
                if (rowCountToClearListBox > 0 && listBox.Items.Count == rowCountToClearListBox)
                    listBox.Items.Clear();

                listBox.Items.Add(message);
                listBox.SelectedIndex = listBox.Items.Count - 1;
            }
        }

        public static void ClearItemsInListBox(ListBox listBox)
        {
            if (listBox.InvokeRequired)
            {
                listBox.Invoke((MethodInvoker)(() => listBox.Items.Clear()));
            }
            else
            {
                listBox.Items.Clear();
            }
        }

        public static void AddMessageToLabel(Label label, string message)
        {
            if (label.InvokeRequired)
            {
                label.Invoke((MethodInvoker)(() => label.Text = message));
            }
            else
            {
                label.Text = message;
            }
        }

        public static void AddValueToProgressBar(ProgressBar progressBar, int value)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.Invoke((MethodInvoker)(() => progressBar.Value = value));
            }
            else
            {
                progressBar.Value = value; 
            }
        }

        public static void AddValueToProgressBar(RadProgressBar progressBar, int value)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.Invoke((MethodInvoker)(() => progressBar.Value1 = value));
            }
            else
            {
                progressBar.Value1 = value;
            }
        }

        public static void AddMaxValueToProgressBar(ProgressBar progressBar, int maxValue)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.Invoke((MethodInvoker)(() => progressBar.Maximum = maxValue));
            }
            else
            {
                progressBar.Maximum = maxValue;
            }
        }

        public static void AddMaxValueToProgressBar(RadProgressBar progressBar, int maxValue)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.Invoke((MethodInvoker)(() => progressBar.Maximum = maxValue));
            }
            else
            {
                progressBar.Maximum = maxValue;
            }
        }

        public static void AddMessageToLabel(RadLabel label, string message)
        {
            if (label.InvokeRequired)
            {
                label.Invoke((MethodInvoker)(() => label.Text = message));
            }
            else
            {
                label.Text = message;
            }
        }

        public static String ConvertImageURLToBase64(String url)
        {
            StringBuilder _sb = new StringBuilder();

            Byte[] _byte = GetImage(url);

            _sb.Append(Convert.ToBase64String(_byte, 0, _byte.Length));

            return _sb.ToString();
        }

        public static byte[] GetImage(string url)
        {
            Stream stream = null;
            byte[] buf;

            try
            {
                WebProxy myProxy = new WebProxy();
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                stream = response.GetResponseStream();

                using (BinaryReader br = new BinaryReader(stream))
                {
                    int len = (int)(response.ContentLength);
                    buf = br.ReadBytes(len);
                    br.Close();
                }

                stream.Close();
                response.Close();
            }
            catch (Exception)
            {
                buf = null;
            }

            return (buf);
        }

        public static string GetDownloadFolder(string folderStory, string folderChapter)
        {
            if (!string.IsNullOrEmpty(folderStory))
                folderStory += "\\";
            if (!string.IsNullOrEmpty(folderChapter))
                folderChapter += "\\";

            var executingFolder = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

            var filePath = System.IO.Path.Combine(executingFolder, "Files\\");
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            var pathStory = System.IO.Path.Combine(filePath + folderStory);
            if (!Directory.Exists(pathStory))
                Directory.CreateDirectory(pathStory);
            var pathChapter = System.IO.Path.Combine(pathStory + folderChapter);
            if (!Directory.Exists(pathChapter))
                Directory.CreateDirectory(pathChapter);

            return pathChapter;
        }

        public static string GenerateFileName(string chapterName, string orderIndex)
        {
            do
            {
                orderIndex = "0" + orderIndex;
            } while (orderIndex.Length < 3);

            return chapterName + "-" + orderIndex;
        }
    }
}
